<template>
  <div>
    <div class="box-card">
      <div class="box-header">
        <el-form label-width="80px">
          <el-row>
            <el-col :span="20">
              <el-form-item label="关键词">
                <el-input
                  v-model="input"
                  style="width: 250px"
                  placeholder="根据编号搜索"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-button class="header-btn" @click="clearInput">清空</el-button>
              <el-button type="primary" class="header-btn" @click="searchInput"
                >搜索</el-button
              >
            </el-col>
          </el-row>
        </el-form>

        <div class="header-content">
          <i class="el-icon-info"></i>
          <span>共{{ total }}条记录</span>
        </div>
      </div>
      <el-table
        :data="tableData"
        style="width: 100%"
        :header-cell-style="{ background: '#f4f4f5' }"
      >
        <el-table-column prop="id" label="编号" width="200px">
        </el-table-column>
        <el-table-column prop="questionType" label="题型">
          <template #default="{ row }">
            <span>{{
              row.questionType === '1'
                ? '单选'
                : row.questionType === '2'
                ? '多选'
                : '简答'
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="directoryName" label="题目编号" width="200px">
          <template #default="{ row }">
            <el-link
              type="primary"
              v-for="(item, index) in row.questionIDs"
              :key="index"
              :underline="false"
              @click="openDialog(item)"
              >{{ item.number }}</el-link
            >
          </template>
        </el-table-column>
        <el-table-column prop="addTime" label="录入时间" width="200px">
        </el-table-column>
        <el-table-column prop="totalSeconds" label="答题时间(s)" width="150px">
        </el-table-column>
        <el-table-column prop="accuracyRate" label="正确率(%)">
        </el-table-column>
        <el-table-column prop="userName" label="录入人"> </el-table-column>
        <el-table-column label="操作" width="150px">
          <template #default="{ row }">
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              class="edit2"
              @click="open(row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="pagesize"
        :total="total"
        layout="prev,pager, next,sizes, jumper"
      >
      </el-pagination>
    </div>
    <!-- 弹出框 -->
    <questionsPreviews
      :dialogFormVisible.sync="dialogFormVisible"
      :info="info"
    ></questionsPreviews>
  </div>
</template>

<script>
import { randoms, removeRandoms, detail } from '@/api/hmmm/questions.js'
import questionsPreviews from '../components/questions-preview.vue'
export default {
  components: {
    questionsPreviews
  },
  data () {
    return {
      input: '',
      tableData: [],
      page: 1,
      pagesize: 20,
      total: 0,
      dialogFormVisible: false,
      titleType: 'add',
      id: '',
      subject: '',
      info: ''
    }
  },

  async created () {
    this.initTable()
  },

  methods: {
    async initTable () {
      const { data } = await randoms({
        page: this.page,
        pagesize: this.pagesize,
        keyword: this.input
      })
      console.log(data)
      this.tableData = data.items
      this.total = data.counts
    },
    // pagesize改变时
    handleSizeChange (val) {
      this.pagesize = val
      this.page = 1
      this.initTable()
    },
    // page改变时
    handleCurrentChange (val) {
      this.page = val
      this.initTable()
    },
    // 清空input
    clearInput () {
      this.input = null
      this.initTable()
    },
    // 搜索input
    searchInput () {
      this.initTable()
    },

    // 删除 取消确认按钮
    open (id) {
      this.$confirm('此操作将永久删除该题组, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          // 发删除请求
          await removeRandoms({ id })
          this.initTable()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作！'
          })
        })
    },
    // 点击题目编号
    async openDialog (item) {
      this.dialogFormVisible = true
      const { data } = await detail({ id: item.id })
      console.log(data)
      this.info = data
    }
  },
  computed: {
    diologTitle () {
      if (this.titleType === 'add') return '新增目录'
      if (this.titleType === 'edit') return '修改目录'
      else return 'add'
    }
  }
}
</script>

<style scoped lang="less">
.box-card {
  margin: 20px;
  background-color: #fff;
  padding: 20px;
  .header-content {
    background-color: #f4f4f5;
    color: #909399;
    font-size: 13px;
    padding-left: 8px;
    height: 40px;
    line-height: 40px;
    margin-bottom: 20px;
    .el-icon-info {
      margin-right: 5px;
    }
  }
}
.edit2 {
  color: #f56c6c;
  background: #fef0f0;
  border-color: #fbc4c4;
  &:hover {
    background-color: #f56c6c;
    color: #fff;
  }
}
.el-pagination {
  height: 40px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding-top: 20px;
}

.el-dialog {
  .el-form {
    width: 400px;
    margin: auto;
    .el-input {
      width: 310px;
      height: 35px;
    }
    .el-select {
      width: 310px;
      height: 35px;
    }
  }
  .dialog-footer {
    margin-top: 60px;
    position: relative;
    left: 230px;
  }
}
</style>
